package interviewClassic150;

public class MinSubArrayLen {
    public int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        //滑动窗口
        int left = 0;
        int right = 0;
        int curSum = nums[0];
        int len = Integer.MAX_VALUE;
        while (right < n || curSum >= target) {
            while (left <= right && curSum >= target) {
                //这里也去判断
                int curLen = right - left + 1;
                len = Math.min(len,curLen);
                curSum -= nums[left];
                left++;
            }
            //现在窗口中总和小于target，则现在去滑动右边界
            right++;
            if (right >= n) break;
            curSum += nums[right];
            if (curSum >= target) {
                //此时刚好满足条件，则计算当前长度，并与结果比较
                int curLen = right - left + 1;
                len = Math.min(len,curLen);
            }
        }

        return len == Integer.MAX_VALUE ? 0 : len;
    }
}
